//管理handler底下的底层Object对象方法

import {
  reactive
} from ".";
import {
  isObject,
  hasOwnPropery,
  isEqual
} from "../shared/utils";

//createGetter() -> return function get(){}
const get = createGetter(),
  set = createSetter(),
  deleteProperty = createDeleteProperty();


function createGetter() {
  return function get(target, key, receiver) {
    //Reflect底下也有Object底层的对象方法
    const res = Reflect.get(target, key, receiver);
    console.log('响应式获取:' + target[key]);
    // 相当于return target[key];

    //如果嵌套的属性仍是对象就再次递归响应式数据处理
    if (isObject(res)) {
      return reactive(res);
    }

    return res;
  }
}

function createSetter() {
  return function set(target, key, value, receiver) {
    const isKeyExist = hasOwnPropery(target, key),
      //拿到修改之前的value
      oldValue = target[key],
      res = Reflect.set(target, key, value, receiver);
    console.log('响应式设置:' + key + ' = ' + value);


    //如果target对象里有key 说明是修改/ 没有则是新增
    if (!isKeyExist) {
      console.log('响应式新增: ' + value);

    } else if (!isEqual(value, oldValue)) {
      //如果新老值不相等，证明修改
      console.log('响应式修改: ' + key + ' = ' + value);
    }

    return res;
  }
}

function createDeleteProperty() {
  return function deleteProperty() {}
}

const mutableHandler = {
  get,
  set,
  deleteProperty
}

export {
  mutableHandler
}